#!/bin/ash -efu
#
# mki-copy-subdirs
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

verbose "has started executing."

subdirs="${SUBDIRS:?subdirectories required}"

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

exit_handler() {
	local rc=$?
	trap - EXIT
	find "$chroot/.in/" -mindepth 1 -maxdepth 1 \
		-execdir rm -rf -- '{}' '+'
	exit $rc
}

trap exit_handler HUP INT QUIT TERM EXIT

for d in $subdirs; do
	outsubdir="$curdir/$d/$workdirname/$outdirname"

	get_copy_args "$outsubdir" "$chroot/.in"

	[ -n "${d##*/*}" ] ||
		d="$(printf '%s\n' "$d" |sed -e 's@/@_@g')"

	mkdir -p -- "$chroot/.in/$d"
	find "$outsubdir" -mindepth 1 -maxdepth 1 \
		-execdir cp $verbose $cp_args -at "$chroot/.in/$d/" -- '{}' '+'

done

mki-run /.host/find "/.in/" -mindepth 2 -maxdepth 2 \
	-execdir cp -alft /.image/ -- '{}' '+'
